<?php
/**
 * Fire PHP Kernel Module 
 *
 * @package Apricot
 */

/**
 * @see Apricot_Kernel_Abstract
 */
require_once 'Apricot/Module/Abstract.php';

/**
 * Fire PHP Kernel module
 *
 * This module allows access to the FirePHP debug log facility.
 * From a controller call it with $this->_kernel->firephp($variable_to_dump);
 *
 * Can also use the following API commands
 *  - $this->_kernel->firephp()->log('Log this');
 *  - $this->_kernel->firephp()->info('Information');
 *  - $this->_kernel->firephp()->warn('Warning message');
 *  - $this->_kernel->firephp()->error('Error message');
 *  - $this->_kernel->firephp()->table('Label', $tableArray);
 *  - $this->_kernel->firephp()->trace('Trace label');
 * 
 * @uses Apricot_Kernel_Abstract
 * @package Apricot
 * @author Jansen Price <jansen.price@gmail.com>
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 * @version ApricotVersion: 1.1
 */
class FirePHPModule extends Apricot_Module_Abstract
{
    /**
     * Whether this should be activated
     *
     * @var bool;
     */
    public static $enabled = true;

    /**
     * Register this module (called when it is loaded)
     * 
     * @return void
     */
    public function register()
    {
        include_once 'lib/FirePHPCore/fb.php';
    }

    /**
     * Handle options
     * 
     * @return void
     */
    public function handleOptions()
    {
        if (isset($this->_options->enabled)
            && $this->_options->enabled
        ) {
            $this->enable();
        }
    }

    /**
     * Disable the Firephp Module
     * 
     * @return void
     */
    public function disable()
    {
        self::$enabled = false;
    }

    /**
     * Enable the Firephp Module
     * 
     * @return void
     */
    public function enable()
    {
        self::$enabled = true;
    }

    /**
     * Direct method when call is invoked
     * 
     * @param array $options Options for method
     * @return mixed
     */
    public function direct($options = null)
    {
        $args = func_get_args();
        return $this->_callFirephp('fb', $args);
    }

    /**
     * Call firephp core method
     * 
     * @param string $method Method name
     * @param array $args Arguments
     * @return mixed
     */
    protected function _callFirephp($method = 'fb', $args = array())
    {
        if (!self::$enabled) {
            return;
        }

        $instance = FirePHP::getInstance(true);
        return call_user_func_array(array($instance, $method), $args);
    }

    /**
     * Magic call method is passed through to FirePHP Core
     * 
     * @param mixed $method Method name
     * @param mixed $args Arguments
     * @return mixed
     */
    public function __call($method, $args)
    {
        return $this->_callFirephp($method, $args);
    }
}
